<script>
	export default {
		globalData: {
			showTickTip: false, //显示提示框
			isFirstLogin: false,
			subscribe: false,
			isConcern: false,
			successPa: false,
			toLogin: false,
			header: {
				Authority: ''
			},
			isHaiZhu: true,
			isPasswordError: false,
			isSameDay: false,
			navBarHeight: 0, // 导航栏高度
			menuBotton: 0, // 胶囊距底部间距（保持底部间距一致）
			menuRight: 0, // 胶囊距右方间距（方保持左、右间距一致）
			menuHeight: 0, // 胶囊高度（自定义内容可与胶囊高度保证一致）
			pageHeight: 0, //页面高度
			menuWidth: 0,
			statusBarHeight: 0, //底部tabbar高度
			screenWidth: 0, //屏幕高度
			isLogin: false,
			newPerson: false,
			firstIn: true,
			toDetail: false,
			tabbarList: []


		},
		async onLaunch() {
			uni.setStorageSync('showTickTip',false);
			// console.log(uni.getStorageSync('showTickTip'))
			uni.hideTabBar()
			let vm = this;

			wx.cloud.init({
				env: "	course-service-0gf7qv1h7d7d6aae"
			});
			// const c1 = new wx.cloud.Cloud({
			//    resourceEnv: 'prod-0g5m8v40aeec4be5',
			//  })
			console.log('c1--------', wx.cloud)
			// await c1.init()
			// uni.setTabBarBadge({
			//   index: 1,
			//   text: ''
			// })


			// setTimeout(() => {
			// 	uni.hideTabBar()
			// 	// uni.setTabBarItem({
			// 	//   index: 1,
			// 	//   iconPath: '/static/icon/shop.png',
			// 	//   selectedIconPath: 'static/icon/shop_selected.png',
			// 	//   success() {
			// 	// 	  console.log('成功')
			// 	//   },
			// 	//   fail(error) {
			// 	//   	console.log('失败',error)
			// 	//   }
			// 	// })
			// 	// uni.setTabBarStyle({
			// 	//   color: '#FF0000',
			// 	//   selectedColor: '#FF0000',
			// 	//   borderStyle: 'white'
			// 	// })
			// }, 1000)
			vm.quickLogin();
			this.setNavBarInfo()
			this.getPagesHeight();
			
			this.tabbar()
		},
		onShow: function() {},
		onHide: function() {},
		methods: {
			async tabbar(){
				// this.showTickTip = trues
				// console.log(this.showTickTip )
				
				// let header = getApp().globalData.header
				let data = await this.UniRequest("/goods/m/tabbarAndTagShowB","GET","","")
				console.log(data,'展示tabbar')
				if (data.data.data == 0) {
					// console.log(data.data.data)
					
						uni.hideTabBar()
				} else {
					uni.showTabBar();
					uni.setStorageSync('showTickTip',true);
					
				}
			},
			watch: function(key, method) {
				var obj = this.globalData;
				//加个前缀生成隐藏变量，防止死循环发生
				getApp().globalData[key + 'tmp'] = obj[key]; //obj[key]这个不能放在Object.defineProperty里
				if (getApp().globalData[key + 'tmp']) { //处理已经声明的变量，绑定处理
					method(getApp().globalData[key + 'tmp']);
				}
				Object.defineProperty(obj, key, {
					configurable: true,
					enumerable: true,
					set: function(value) {
						getApp().globalData[key + 'tmp'] = value;
						method(value);
					},
					get: function() {
						return getApp().globalData[key + 'tmp']
					}
				})
			},
			async UniRequest(path, method, body, header) {
				// let dataReq = new Object()
				// dataReq.config = {
				// 	env: 'course-service-0gf7qv1h7d7d6aae'
				// }
				// dataReq.path = "/zkCourse"+path
				// dataReq.method = method
				// if (body) {
				// 	dataReq.data = body
				// }
				// dataReq.header = {
				// 	'X-WX-SERVICE': 'course'
				// }
				// if (header) {
				// 	dataReq.header.Authority = header.Authority
				// }
				// // console.log(dataReq)
				// let {
				// 	data
				// } = await wx.cloud.callContainer(dataReq);
				// // console.log('ffff',data)
				// return data

				let data
				let request = new Object
				// request.url = "http://119.91.78.179:9001"+path
				// request.url = "http://119.91.78.179:9001"+path;
				// request.url = "http://112.74.188.40:9001/zkCourse" + path;
				request.url = "https://www.zcjn.top/zkCourse" + path
				// request.url = "http://120.77.183.115/zkCourse" + path
				// request.url = "https://course-1383871-1308162715.ap-shanghai.run.tcloudbase.com"+path
				// request.url = "http://159.75.241.252:9001/zkCourse"+path
				request.method = method
				if (body) {
					request.data = body;
					// console.log(request.data,'请求参数')
				}
				if (header) {
					request.header = header
				}
				await uni.request(request).then(res => {
					data = res
				})
				return data[1].data
			},
			async officailRequest(path, method, body, header) {
				let data
				let request = new Object
				//request.url = "https://127.0.0.1"+path
				request.url = "https://www.zcjn.top/official" + path
				request.method = method
				if (body) {
					request.data = body
				}
				if (header) {
					request.header = header
				}
				await uni.request(request).then(res => {
					data = res
				})
				return data[1].data
			},
			async quickLogin(userInfo) {
				let vm = this
				console.log('quickLogin')
				wx.login().then(async (res) => {
					let body = new Object()
					body.code = res.code;
					console.log(res, '登录结果')
					if (userInfo) {

						body.nickName = userInfo.nickName
						body.avatarUrl = userInfo.avatarUrl
						body.gender = userInfo.gender
					}
					let data = await getApp().UniRequest("/user/login", "POST", body, "")
					/* let data1 = await getApp().UniRequest("/twohandbook/user/quicklyLogin","POST",body,"");
					console.log('data1',data1) */
					//快捷登录失败则跳转到登录页
					if (data.code == 20002) {
						//console.log("跳转登录页")
						// uni.navigateTo({
						// 	url: "../login/login"
						// })
						uni.reLaunch({
							url: "../login/login"
						})
						// uni.reLaunch({
						// 	url: "../maintainTip/maintainTip"
						// })
					} else {
						console.log('结果11', data);
						let authority = data.data.Authority
						uni.setStorageSync("Authority", authority);
						getApp().globalData.firstIn = false;
						let header = new Object()
						header.Authority = authority
						// getApp().globalData.isLogin = true
						// console.log('登录',getApp().globalData.isLogin)
						getApp().globalData.header = header
						// let res = await getApp().UniRequest("/user/deleteLoginCache", "Get", {openid: 'ohpVk5SF3oodWNsodPzlmbiiHPXY'},header)
						// console.log('删除',res)
						// uni.request({
						// 	url: 'http://119.91.78.179:9001/countDown/insertRenWenCountDown?xueHao=202010244304', //仅为示例，并非真实接口地址。
						// 	header: getApp().globalData.header,
						// 	method: 'GET',
						// 	success: (res) => {
						// 		console.log('人文',res);
						// 		// this.text = 'request success';
						// 	}
						// });
						vm.passwordCheck() //快捷登录，但是可能出现密码错误
						let userInfo = new Object()
						userInfo.avatarUrl = data.data.avatarUrl
						userInfo.nickName = data.data.nickName
						userInfo.gender = data.data.gender
						uni.setStorageSync("userInfo", userInfo)
						getApp().globalData.subscribe = data.data.subscribe
						getApp().globalData.isHaiZhu = data.data.isHaiZhu
						//console.log("isHaizhu:"+getApp().globalData.isHaiZhu)
						if (data.code == 20005) {
							//重新获取用户信息
							uni.showModal({
								title: '小程序需要您的信息',
								showCancel: false
							}).then(res => {
								uni.getUserProfile({
									desc: '获取用户基本信息'
								}).then(async (res) => {
									uni.setStorageSync("userInfo", res[1].userInfo);
								})
							})
						}

					}
				})
			},
			// async triCountDown() {
			// 	uni.showLoading({
			// 		title: '请稍等..',
			// 		// mask: true
			// 	})
			// 	let header = getApp().globalData.header
			// 	let data = await getApp().UniRequest("/countDown/authority/triCountDown", "POST", "", header);
			// 	console.log('课程', data)
			// 	if (data.code == 20003) {
			// 		getApp().globalData.isPasswordError = true
			// 	}
			// 	uni.hideLoading()
			// },
			async passwordCheck() {
				let header = getApp().globalData.header
				let data = await getApp().UniRequest("/user/passwordCheck/authority", "POST", "", header)
				if (data.code == 20003) {
					getApp().globalData.isPasswordError = true
				} else {
					console.log("密码是对的")
				}
			},

			setNavBarInfo() {
				// 获取系统信息
				const systemInfo = wx.getSystemInfoSync();
				// 胶囊按钮位置信息
				const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
				// 导航栏高度 = 状态栏到胶囊的间距（胶囊距上距离-状态栏高度） * 2 + 胶囊高度 + 状态栏高度
				this.globalData.navBarHeight = (menuButtonInfo.top - systemInfo.statusBarHeight) * 2 + menuButtonInfo
					.height + systemInfo.statusBarHeight;
				this.globalData.navBarHeight = this.globalData.navBarHeight * 750 / wx.getSystemInfoSync().windowWidth;
				console.log(this.globalData.navBarHeight)
				this.globalData.menuBotton = menuButtonInfo.top - systemInfo.statusBarHeight;
				this.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
				this.globalData.menuHeight = menuButtonInfo.height;
				this.globalData.menuWidth = menuButtonInfo.width;
				this.statusBarHeight = systemInfo.statusBarHeight;
				this.screenWidth = systemInfo.screenWidth;
				console.log(111, this.screenWidth)
				// var rpx = px * 750 / wx.getSystemInfoSync().windowWidth;
				// console.log( this.globalData.navBarHeight);
				// console.log( this.globalData.menuBotton);
				// console.log( this.globalData.menuRight);
				// console.log(this.globalData.menuHeight)
			},

			getPagesHeight() {
				let that = this;
				uni.getSystemInfo({
					success: function(res) {
						let pxToRpxScale = 750 / res.windowWidth;
						that.globalData.pageHeight = res.windowHeight * pxToRpxScale;
					}
				});
				console.log(this.globalData.pageHeight, '页面高度')
			}

		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "uview-ui/index.scss";
	@import "./static/icon/iconfont/iconfont.css";

	button {
		background-color: #fff;
		border: 0rpx;
		padding-right: 10rpx;
		padding-left: 10rpx;
	}
</style>
